home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5947 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.8 KB

  1. Path: pegasus.montclair.edu!harmon
  2. From: harmon@pegasus.montclair.edu (Derek Harmon)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Ability to locate spaces?
  5. Date: 16 Feb 1996 01:06:54 -0500
  6. Organization: Montclair State University
  7. Message-ID: <harmon.824449354@pegasus.montclair.edu>
  8. References: <Pine.SOL.3.91.960215222301.15979A-100000@teer1.acpub.duke.edu>
  9. NNTP-Posting-Host: pegasus.montclair.edu
  10. X-Newsreader: NN version 6.5.0 #68 (NOV)
  11.  
  12. John Young Oh <jyo@acpub.duke.edu> writes:
  13.  
  14. >I would like the program to be able to recognize that there are spaces
  15. >between the numbers and keep track of them. What I did was to use
  16. >fgets and read in the line into a string. I then used a for loop
  17. >and checked each array element of the character string and used an
  18. >if statement to see if an array element was a space.
  19.  
  20.    Another alternative might be to check out strchr() in <string.h>.
  21.  
  22. >FILE *input;
  23. >char *string, buf[200];
  24.  ^ At this point, string is an uninitialized pointer, and buf is, when used
  25.  without square brackets, effectively a pointer to a statically allocated 
  26.  200 bytes.
  27.  
  28. >int i, count;
  29. >input = fopen ("data", "r");
  30.  ^ best to test fopen()'s return value with NULL to make certain everything
  31.  has gone well.
  32.  
  33. >(etc etc etc)
  34.  ^ this should've caused some type of syntax error.  :D
  35.  
  36. >string = fgets (buf, 199, input);
  37.  ^ BANG!  Unless you snuck a string = (char *)malloc( ... somewhere before
  38.  this point, string remains an uninitialized pointer.  It's worth noting that
  39.  fgets() returns the same string that it reads into buf.  There is no reason
  40.  to keep its return value here, and you could discard it with,
  41. : fgets(buf, 200, input);  /* You don't use buf as a string, so you are */
  42. :                          /* technically allowed all 200 bytes if you */
  43. :                          /* don't intend to (otherwise buf[199] = '\0' */
  44. :                          /* is a good idea). */
  45.  
  46. >for (i = 0; i < 40; ++i)
  47. >{
  48. >    if (buf[i] == ' ')
  49. >    {
  50. >        ++count;
  51. >    }
  52. >}
  53.  ^ initializing count to zero is always prudent, although it it is statically
  54. declared globally you could rely on the compiler (but not if it is within a
  55. function).  Best advice, initialize count = 0;  This routine will count the
  56. spaces in the first forty bytes of buf[].  The braces here are also super-
  57. fluous, they aren't needed to envelope single statements, but that's purely
  58. a stylistic point.
  59.  
  60.    But, all told, looks like another victim of the viscious Uninitialized
  61. Pointer (and the tragic thing is you don't appear to need string for any-
  62. thing).  :)
  63.                                                      -- Stone
  64. --
  65. # Derek Harmon (aka Stonelight)    harmon@pegasus.montclair.edu
  66. # - Computer Science Undergrad, Montclair State University, NJ
  67. # - My views are my own, nobody else is this creative.  3;)>
  68. ... SYNTAX?  Why not, they already tax everything else!
  69.  
  70.